package cn.luckdou.PC.Hcontroller;

import cn.luckdou.commons.utils.StringEscapeEditor;
import cn.luckdou.system.model.country.Country;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

public class HbaseController {
    public final String noPowerUrl="/personal/noPower";


    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /**
         * 防止XSS攻击
         */
        binder.registerCustomEditor(String.class, new StringEscapeEditor());
    }
    /**
     * redirect跳转
     *
     * @param url
     *            目标url
     */
    public String redirect(String url) {
        return new StringBuilder("redirect:").append(url).toString();
    }

    /**
     * forward 转发
     *
     * @param url
     *            目标url
     */
    public String forward(String url) {
        return new StringBuilder("forward:").append(url).toString();
    }



    /**
     * 从cookic中获取Country
     * @param request
     * @return
     */
    public Long getCountryId(HttpServletRequest request){
        try{
            Cookie[] cookies = request.getCookies();
            if (cookies!=null){
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("country")){
//                        String s= URLDecoder.decode(cookie.getValue(),"UTF-8");
                        String s=cookie.getValue();
                        String[] strings = s.split("%7C");
                        return Long.parseLong(strings[0]);
                    }
                }
            }
        }catch (Exception e){
            return null;
        }
        return null;
    }

    /**
     * 将Country写入cookic
     * @param country
     */
    public void setCountryToCookic(Country country, HttpServletResponse response) {
        try {
            String value = country.getId() + "|" + country.getCaname() + "|" + country.getImgUrl() + "?x-oss-process=style/country";
            value = URLEncoder.encode(value, "utf-8");
            Cookie cookie = new Cookie("country", value);
            cookie.setPath("/");
            cookie.setMaxAge(365*24*60*60);
            response.addCookie(cookie);
        }catch (Exception e){

        }
    }






    public void download(HttpServletResponse response,String fileName, ByteArrayOutputStream os )throws Exception{
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            InputStream is = new ByteArrayInputStream(os.toByteArray());
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
    }
}
